package com.itmumu.miniwechat.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmumu.miniwechat.common.domain.response.ApiResult;
import com.itmumu.miniwechat.user.domain.dto.SearchContactVo;
import com.itmumu.miniwechat.user.domain.dto.UserContactApplyDto;
import com.itmumu.miniwechat.user.domain.vo.request.UserContactApplyQuery;
import com.itmumu.miniwechat.user.domain.vo.response.PageResultVo;
import com.itmumu.miniwechat.user.domain.vo.response.UserContactGroupVo;
import com.itmumu.miniwechat.user.domain.vo.response.UserContactVo;
import com.itmumu.miniwechat.user.service.UserContactApplyService;
import com.itmumu.miniwechat.user.service.UserContactService;
import com.itmumu.miniwechat.user.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/contact")
@Slf4j

public class UserContactController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserContactService userContactService;

    @Autowired
    private UserContactApplyService userContactApplyService;

    @PostMapping("/searchContact")
    public ApiResult<Map<String, Object>> searchContact(@RequestBody SearchContactVo searchContactVo) {
        Integer uid = userService.searchContact(searchContactVo);

        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        return ApiResult.success(map);
    }


    @PostMapping("/applyAdd")
    public ApiResult applyAdd(@Validated @RequestBody UserContactApplyDto applyDto) {
        log.info("{}", applyDto);
        userContactApplyService.applyAdd(applyDto);

        return ApiResult.success();
    }

    @PostMapping("/listContactApply")
    public ApiResult<PageResultVo<UserContactVo>> listContactApply(@RequestBody UserContactApplyQuery query) {
        PageResultVo<UserContactVo> result = userContactApplyService.listContactApply(query);
        return ApiResult.success(result);
    }


    @RequestMapping("/dealWithApply")
    public ApiResult dealWithApply(@NotNull @RequestBody  HashMap<String, Integer> map) {
        Integer applyId = map.get("applyId");
        userContactApplyService.dealWithApply(applyId);
        return ApiResult.success().message("添加成功");
    }


    @GetMapping("/listUserContact")
    public ApiResult listUserContact() {

        UserContactGroupVo groupVo = userContactService.listUserContact();
        return ApiResult.success(groupVo);
    }

}
